layui.config({
    base: '/static/cropper/' //静态资源所在路径
    // base: '../../static/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['croppers','jquery','layer','form','laypage'], function() {
    var laypage = layui.laypage;
    var $ = layui.jquery;
    var form = layui.form;
    var croppers = layui.croppers;
    var layer = layui.layer; //独立版的layer无需执行这一句
    var adminId=$.cookie("adminId");
    //获取登录昵称
    $.ajax({
        url:"/admin/myInfo",
        type:"get",
        data:{id:adminId},
        success:function (result) {
            var user=result.data;
            $("#id").attr('value',user.id);
            $("#username").attr('value',user.username);
            $("#realname").attr('value',user.realname);
            $("#phone").attr('value',user.phone);
            if (user.avatar !=null && user.avatar !=''){
                $('#avatarUrl').attr('src', user.avatar);
                $('#avatar').val( user.avatar);  //成功返回路径存到数据库
            }
            var man=$("<input>",{
                "title":"男",
                "value":"0",
                "type":"radio",
                "name":"gender"
            });
            var woman=$("<input>",{
                "title":"女",
                "value":"1",
                "type":"radio",
                "name":"gender"
            });
            if(user.gender){
                woman.attr("checked",true);
            }else {
                man.attr("checked",true);
            }
            $("#isGender").text("");
            $("#isGender").append(man);
            $("#isGender").append(woman);
            $(".disabled").attr("disabled",true);
            $(".disabled").css("background-color","#d8dcdb");
            form.render();

        },
        error:function () {
            layer.msg("请求失败", {icon: 2,time:2000,shade:0.2});
        }
    });


    croppers.render({
        elem: '#test1'
        ,saveW:150     //保存宽度
        ,saveH:150   //保存高度
        ,mark:1/1    //选取比例
        ,area:["900px","680px"]  //弹窗宽度
        ,url: "/common/upload"  //图片上传接口返回和（layui 的upload 模块）返回的JOSN一样
        ,done: function(data){ //上传完毕回调
            $('#avatarUrl').attr('src', data);
            $('#avatar').val( data);  //成功返回路径存到数据库
        }
    });

    //监听提交
    form.on('submit(demo1)', function(data){
        var data=data.field;
        var regPhone = /^1[345789]\d{9}$/;
        if(data.realname==null||data.realname==''){
            layer.msg("请输入用户名称", {icon: 2,time:2000,shade:0.2});
            return false;
        }else if(data.phone==null||data.phone==''){
            layer.msg("请输入电话", {icon: 2,time:2000,shade:0.2});
            return false;
        }else if(!regPhone.test(data.phone)){
            layer.msg("请输入正确的电话号码", {icon: 2,time:2000,shade:0.2});
            return false;
        }else {
            $.ajax({
                url:"/admin/addOrEdit",
                type:"post",
                data:data,
                success:function (result) {
                    if(result.code==0){
                        layer.msg(result.message, {icon: 1,time:2000,shade:0.2});
                        setInterval(function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index);
                            window.parent.location.reload();
                        },500)
                    }else {
                        layer.msg(result.message, {icon: 2,time:2000,shade:0.2});
                    }

                }
            });
        }
        return false;
    });

})
